package org.hxyjs.dao.impl;

import org.hxyjs.dao.GameDetailsRecordDao;
import org.hxyjs.entity.GameDetailsRecord;
import org.hxyjs.mapper.GameDetailsRecordMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("gameDetailsRecordDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class GameDetailsRecordDaoImpl implements GameDetailsRecordDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private GameDetailsRecordMapper gameDetailsRecordMapper;

    //查询全部
    public List<GameDetailsRecord> getAllGameDetailsRecords() throws Exception {
        return gameDetailsRecordMapper.selectByExample(null);
    }

    //按主键查询
    @Override
    public GameDetailsRecord getGameDetailsRecordByGdrno(String gdrno) throws Exception {
        return gameDetailsRecordMapper.selectByPrimaryKey(gdrno);
    }

    //分页查询
    @Override
    public List<GameDetailsRecord> getGameDetailsRecordByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) gameDetailsRecordMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return gameDetailsRecordMapper.getGameDetailsRecordByPage(map);
    }

    //删除
    @Override
    public Integer deleteGameDetailsRecordByGdrno(String gdrno) {
        return gameDetailsRecordMapper.deleteByPrimaryKey(gdrno);
    }

    //修改
    @Override
    public Integer updateGameDetailsRecordByGdrno(GameDetailsRecord gameDetailsRecord) {
        return gameDetailsRecordMapper.updateByPrimaryKey(gameDetailsRecord);
    }


    //增加
    @Override
    public Integer addGameDetailsRecord(GameDetailsRecord gameDetailsRecord) {
        gameDetailsRecord.setGdrno(initId());
        return gameDetailsRecordMapper.insertSelective(gameDetailsRecord);
    }

    public String initId() {
        String maxGdrno = gameDetailsRecordMapper.getMaxGdrno();
        maxGdrno = Optional.ofNullable(maxGdrno).orElse("0000");
        return GenNumTools.initId("gd", 4, maxGdrno);
    }


}